package com.weixin.gong.example.apache.commons.beanutils;

import org.apache.commons.beanutils.BeanUtils;
import org.junit.Assert;
import org.junit.Test;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author weixin.gong
 * @date 17-5-10 下午2:31
 */
public class MapToBeanExample {

    //BeanUtils操作的类必须有public修饰符
    public static class Bean {
        private int value;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return "Bean{" +
                    "value=" + value +
                    '}';
        }
    }

    @Test
    public void mapToBean() throws InvocationTargetException, IllegalAccessException, NoSuchMethodException {
        Bean bean = new Bean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("value", 1);
        BeanUtils.populate(bean, map);
        System.out.println("MapToBean:" + bean);
        Assert.assertEquals(bean.getValue(), map.get("value"));
    }
}
